package com.amigo.online.provider.sync.abs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.ConstantValue;

/**
 * @version: V 1.0.0
 * @Description: （对类进行功能描述）
 * @author: zxy QQ:1363320658
 * @date: 2018年12月10日 下午2:46:39
 */
@Service
public class DataHandlerBroker {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    TableHandler tableHandler;

    protected static Logger logger = LoggerFactory.getLogger(DataHandlerBroker.class);

    /**
     * 表名后缀长度
     */
    private static final int TABLE_SUFFIX_LENGTH = 4;

    /**
     * @throws
     * @Title: processingDistribution
     * @Description: (根据)
     * @param: @param tableName
     * @param: @param type
     * @param: @param newObject
     * @param: @param oldData
     * @return: void
     */
    public void processingDistribution(String tableName, String type, JSONObject newObject, JSONObject oldData) {
        if (oldData != null) {
            tableHandler.updateHandler(newObject, oldData.keySet(), subTableString(tableName));
        } else {
            if (ConstantValue.TBL_INSERT.equals(type)) {
                tableHandler.insertHandler(newObject, subTableString(tableName));
            } else {
                logger.error("存在非法操作，有数据被实体删除！删除的表名为:{},删除的数据为:{}", tableName, newObject.toString());
            }
        }
    }

    private static String subTableString(String tableName) {
        return tableName.substring(0, tableName.length() - TABLE_SUFFIX_LENGTH);
    }

}
